//
//  MyInfoViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/5/5.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "MyCarsListViewController.h"
#import "AddModifyCarViewController.h"
#import "SVProgressHUD.h"
#import "HttpClientManager.h"
#import "DataModel.h"
#import "AppDelegate.h"
#import "AppMacro.h"

@interface MyCarsListViewController ()

@end

@implementation MyCarsListViewController{
    
    NSMutableArray *carsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.loginModel = ApplicationDelegate.loginModel;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [self requestMyCarsList];
}

#pragma mark - TableViewDelegate && DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return carsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 61;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *carCellIndentifier = @"MyInfoCarInfoCell";
    
    NSInteger row = indexPath.row;
    MyCarListModel *carModel = carsArray[row];
    MyInfoCarInfoCell *carCell = [tableView dequeueReusableCellWithIdentifier:carCellIndentifier];
    [carCell initDataWithCarListModel:carModel];
    return carCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    MyCarListModel *carModel = carsArray[row];
    
    AddModifyCarViewController *addModifyCarViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"AddModifyCarViewController"];
    addModifyCarViewController.isModeify = YES;
    addModifyCarViewController.carModel = carModel;
    [self.navigationController pushViewController:addModifyCarViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBActions

- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)addNewCarAction:(id)sender {
    
    AddModifyCarViewController *addModifyCarViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"AddModifyCarViewController"];
    addModifyCarViewController.isModeify = NO;
    [self.navigationController pushViewController:addModifyCarViewController animated:YES];
}

#pragma mark - 获取我的车牌列表
- (void)requestMyCarsList{
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [[HttpClientManager sharedClient] myCarsListRequestWithUserId:self.loginModel.userId WithSucessBlock:^(JsonRetMsg *response) {
        
        [SVProgressHUD dismiss];
        carsArray = response.data;
        [self.tableView reloadData];
    } withFailedBlock:^(JsonRetMsg *responseObject) {
        
    }];
}
@end
